package h0812;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author 不知名帅哥
 * @version 1.0
 * @description 欢乐的周末
 * @date 2024/8/12 21:52
 */
public class HappyWeekend {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //地图的长和宽
        int m = sc.nextInt();
        int n = sc.nextInt();

        int[][] matrix = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                matrix[i][j] = sc.nextInt();
            }
        }

        System.out.println(getResult(matrix));
    }

    private static int getResult(int[][] matrix) {
        int rows = matrix.length;
        int cols = matrix[0].length;

        UnionFindSet ufs = new UnionFindSet(rows * cols);
        //记录小华小为的位置
        ArrayList<Integer> huawei = new ArrayList<>();
        ArrayList<Integer> restaurants = new ArrayList<>();

        //上下左右四个偏移量
        int[][] offsets = {
                {-1, 0}, {1, 0}, {0, -1}, {0, 1}
        };
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (matrix[i][j] != 1) {
                    //二维转一维  TODO 关键步骤，这样就可以使用并查集了
                    int pos = i * cols + j;
                    if (matrix[i][j] == 2) {
                        huawei.add(pos);
                    } else if (matrix[i][j] == 3) {
                        //收集餐厅的位置
                        restaurants.add(pos);
                    }


                    for (int[] offset : offsets) {
                        int newI = i + offset[0];
                        int newJ = j + offset[1];
                        if (newI >= 0 && newJ >= 0 && newI < rows && newJ < cols && matrix[newI][newJ] != 1) {
                            ufs.union(pos, newI * cols + newJ);
                        }
                    }
                }
            }
        }

        int pos1 = ufs.findParent(huawei.get(0));
        int pos2 = ufs.findParent(huawei.get(1));
        if (pos1 != pos2) {
            return 0;
        }

        int ans = 0;
        for (Integer restaurant : restaurants) {
            int restPos = ufs.findParent(restaurant);
            //如果餐厅跟两个人在同一个集合，就可以到达
            if (restPos == pos1) {
                ans++;
            }
        }
        return ans;
    }


    //并查集
    static class UnionFindSet {
        int[] fa;

        public UnionFindSet(int n) {
            fa = new int[n];
            for (int i = 0; i < n; i++) {
                fa[i] = i;
            }
        }

        public int findParent(int x) {
            if (x == fa[x]) {
                return x;
            }
            //路径压缩
            return fa[x] = findParent(fa[x]);
        }

        //合并操作 TODO 这里注意合并操作 要用findParent;
        public void union(int x, int y) {
            int pa_x = findParent(x);
            int pa_y = findParent(y);
            if (pa_x != pa_y){
               this.fa[pa_y] = pa_x;
            }
        }
    }
}
